﻿#region

using System;
using System.Windows;

#endregion

namespace Radovici.Ctrls.ContentControls
{
    public class Dropable : Dragable
    {
        public event Action<Dropable> DropStarted;
        public event Action<Dropable> DropStopped;

        public object Source
        {
            get { return SourceDataContext ?? DataContext; }
        }

        #region DropEnabled

        // Using a DependencyProperty as the backing store for DropEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DropEnabledProperty = DependencyProperty.Register("DropEnabled",
                                                                                                    typeof (bool),
                                                                                                    typeof (Dropable),
                                                                                                    new PropertyMetadata
                                                                                                        (true,
                                                                                                         OnDropEnabledChanged));

        public bool DropEnabled
        {
            get { return (bool) GetValue(DropEnabledProperty); }
            set { SetValue(DropEnabledProperty, value); }
        }

        private static void OnDropEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #region SourceDataContext

        // Using a DependencyProperty as the backing store for SourceDataContext.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("SourceDataContext",
                                                                                               typeof (object),
                                                                                               typeof (Dropable),
                                                                                               new PropertyMetadata(null));

        public object SourceDataContext
        {
            get { return GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        #endregion

        #region Protected methods

        protected virtual void OnDropStarted()
        {
            DropTarget.Monitor(this);
            if (DropStarted != null)
            {
                DropStarted(this);
            }
        }

        protected virtual void OnDropStopped()
        {
            if (DropStopped != null)
            {
                DropStopped(this);
            }
        }

        protected override void OnDragStarted()
        {
            base.OnDragStarted();
            OnDropStarted();
        }

        protected override void OnDragStopped()
        {
            base.OnDragStopped();
            OnDropStopped();
        }

        #endregion
    }
}